package com.phkj.qdh.ui.fragment.usecar

import android.os.Build
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.annotation.RequiresApi
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModelProvider
import com.phkj.qdh.R
import com.phkj.qdh.bean.UseCarBean
import com.phkj.qdh.bean.UseCarMyBean
import com.phkj.qdh.databinding.FragmentUseCarDetailMyBinding
import com.phkj.qdh.utils.launchAndRepeatWithViewLifecycle
import kotlinx.coroutines.flow.collectLatest

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 提交人查看自己的用车申请
 */
class UseCarDetailMyFragment : Fragment() {
    private var _binding: FragmentUseCarDetailMyBinding? = null
    private val binding get() = _binding!!
    private var param1: UseCarMyBean? = null
    private var param2: String? = null
    private lateinit var vm: CarVM
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getParcelable(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
        requireActivity().onBackPressedDispatcher.addCallback(
            this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    parentFragmentManager.popBackStack()
                }
            })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentUseCarDetailMyBinding.inflate(inflater, container, false)
        vm = ViewModelProvider(this, getCarVMProvider())[CarVM::class.java]
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.run {
            ivBack.setOnClickListener {
                parentFragmentManager.popBackStack(
                    "my_detail", FragmentManager.POP_BACK_STACK_INCLUSIVE
                )
            }
        }
        launchAndRepeatWithViewLifecycle {
            vm.useCarMyDetail.collectLatest {
                binding.run {
                    tvCar.text =
                        it.ysNameList.toString().replace(",", "、").replace("[", "").replace("]", "")
                    tvStartDate.text = it.startTimeStr
                    tvEndDate.text = it.endTimeStr
                    edtReson.text = it.applyRemark
//                    tvName.text = it.approverNameList.toString().replace(",","、").replace("[","").replace("]","")

                    when (it.applyStatus) {
                        0 -> {
                            tvState.setBackgroundResource(R.drawable.shape_usecarstate_wait)
                            tvState.text = "待审批"
                        }

                        1 -> {
                            tvState.setBackgroundResource(R.drawable.shape_usecarstate_ok)
                            tvState.text = "已同意"
                        }

                        else -> {
                            tvState.setBackgroundResource(R.drawable.shape_usecarstate_no)
                            tvState.text = "已拒绝"
                        }
                    }
                }
            }
        }
        getDetail()
    }

    private fun getDetail() {
        val map = HashMap<String, Any?>()
        map["applyId"] = param1?.applyId
        vm.getUseCarMyDetail(map)
    }

    companion object {
        /**
         */
        @JvmStatic
        fun newInstance(param1: UseCarMyBean, param2: String) = UseCarDetailMyFragment().apply {
            arguments = Bundle().apply {
                putParcelable(ARG_PARAM1, param1)
            }
        }
    }
}